mui.init();



mui.plusReady(function(){
	
	loaddataSerucrity();
	var name = plus.storage.getItem(KeyCfg.userName);
	$("#name").val(name);
	var tran_question = CmwMcore.$('tran_question');
	
	MeventMgr.onTouchstart(tran_question,function(event,self){
		var a= document.getElementById("a");
		a.checked=true;
		$(".qcode").show(); //原安全问题
		$(".qypwd").hide(); //手机验证+身份证号码
		$(".emailpwd").hide(); //邮箱验证+身份证号码
		$(".resetpwd").hide();	//提交新安全问题	
	});
	
	var phone_card = CmwMcore.$('phone_card');
	//document.querySelector('#phone_card1').addEventListener('#phone_card1',function(e){
		
		//});
	MeventMgr.onTouchstart(phone_card,function(event,self){
		$(".qcode").hide();
		$(".qypwd").show();
		var b= document.getElementById("b");
		b.checked=true;
		$(".emailpwd").hide();
		$(".resetpwd").hide();
	});
	
	var email_card = CmwMcore.$('email_card');
	MeventMgr.onTouchstart(email_card,function(event,self){
		var c= document.getElementById("c");
		c.checked=true;
		$(".qcode").hide();
		$(".qypwd").hide();
		$(".emailpwd").show();
		$(".resetpwd").hide();
	});
	
	/**------- 原安全问题   -------*/
	var btn_question = CmwMcore.$('btn_question');
	MeventMgr.onTouchstart(btn_question,function(event,self){
		if(!valid()) return;
		submitForm();
	});
	
	/**------- 邮箱验证码+身份证  -------*/
	var btn_emailpwd = CmwMcore.$('btn_emailpwd');
	MeventMgr.onTouchstart(btn_emailpwd,function(event,self){
		if(!valid1()) return;
		submitForm1();
	});
	
	/**------- 手机验证码+身份证  -------*/
	var btn_phone = CmwMcore.$('btn_phone');
	MeventMgr.onTouchstart(btn_phone,function(event,self){
		if(!valid2()) return;
		submitForm2();
	});
	
	/**------- 提交新安全问题  -------*/
	var btn_submit = CmwMcore.$('btn_submit');
	MeventMgr.onTouchstart(btn_submit,function(event,self){
		if(!valid3()) return;
		submitForm3();
	});
	
	function submitForm(){
		var questionone = $("#questionone1").val().trim();
		var questiontwo = $("#questiontwo2").val().trim();
        var answer1_val = $("#answer11").val().trim();
		var answer2_val = $("#answer22").val().trim();		
		var url =UrlConfig.sourceSafeAnswer;
		var pars ={answer1:answer1_val,answer2:answer2_val,questionone:questionone,questiontwo:questiontwo};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
					mui.alert("验证成功");
					loadQuestion();
					$(".qbox").hide();
					$(".qcode").hide();
					$(".qypwd").hide();
					$(".emailpwd").hide();
					$(".resetpwd").show();
				}else {
					mui.alert("安全问题回答错误!");
				}
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
	
	
	function submitForm1(){
		var card = $("#card1").val().trim();
		var emailcode =$("#emailcode").val().trim();
		var url =UrlConfig.checkEmailCard;
		var pars={card:card,emailcode:emailcode};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
                	mui.alert("验证成功");
                	loadQuestion();
                	$(".qbox").hide();
                    $(".qcode").hide();
					$(".qypwd").hide();
					$(".emailpwd").hide();
					$(".resetpwd").show();
                } else if (status == "errorCard") {
                	mui.alert("身份证号码错误");                	
                } else if (status == "errorEmail") {
                	mui.alert("邮箱验证码错误");            	
                } else {
                	mui.alert("验证失败");               	
                }
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
	
	function valid(){
		var answer1_val = $("#answer11").val().trim();
		var answer2_val =$("#answer22").val().trim();		
		if(!answer1_val || !answer1_val.trim()){
			mui.alert("答案一不能为空!");
			return false;
		}
		if(!answer2_val || !answer2_val.trim()){
			mui.alert("答案二不能为空!");
			return false;
		}

		
		return true;
	}
	
	function valid1(){
		var cardid_val = $("#card1").val().trim();
		var emailcode_val =$("#emailcode").val();
		
		if(!cardid_val || !cardid_val.trim()){
			mui.alert("请输入您的身份证号!");
			return false;
		}
		
		if(!emailcode_val || !emailcode_val.trim()){
			mui.alert("请输入验证码!");
			return false;
		}
		
		var statuss = false;
		card = cardid_val.trim();
		if (card.length == 15 || card.length == 18) {
			//中国身份证 15位或者18位
			statuss = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card);
		} else if (/(^[A-Z]$)/.test(card.substr(0, 1))) {
			//香港身份证以字母开头，只验证是否以字母开头，不验证长度
			statuss = true;
		} else if (card.length == 8) {
			//港澳身份证
			statuss = /(^[A-Z0-9]\d{6}[A-Z0-9]$)/.test(card);
		}
		if(!statuss){
			mui.alert("身份证号码格式错误!");
			return false; 
		}
		return true;
	}
	
	function valid2(){
		var cardid_val = $("#card2").val().trim();
		var phonecode_val =$("#phonecode").val();
		
		if(!cardid_val || !cardid_val.trim()){
			mui.alert("请输入您的身份证号!");
			return false;
		}
		
		if(!phonecode_val || !phonecode_val.trim()){
			mui.alert("请输入验证码!");
			return false;
		}
		
		var statuss = false;
		card = cardid_val.trim();
		if (card.length == 15 || card.length == 18) {
			//中国身份证 15位或者18位
			statuss = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card);
		} else if (/(^[A-Z]$)/.test(card.substr(0, 1))) {
			//香港身份证以字母开头，只验证是否以字母开头，不验证长度
			statuss = true;
		} else if (card.length == 8) {
			//港澳身份证
			statuss = /(^[A-Z0-9]\d{6}[A-Z0-9]$)/.test(card);
		}
		if(!statuss){
			mui.alert("身份证号码格式错误!");
			return false; 
		}
		return true;
	}
	
	
	
	function valid3(){
		var q1_val = $("#questionone").val().trim();
		var q2_val = $("#questiontwo").val().trim();
		if(!q1_val || q1_val == "--请选择--"){
			mui.alert("请选择问题一!");
			return false;
		}
		
		if(!q2_val || q2_val == "--请选择--"){
			mui.alert("请选择问题二!");
			return false;
		}
		
		var n1_val = $("#answerone").val().trim();
		var n2_val = $("#answertwo").val().trim();
		
		if(!n1_val){
			mui.alert("请输入问题一的答案!");
			return false;
		}
		
		if(!n2_val){
			mui.alert("请输入问题二的答案!");
			return false;
		}
		
		return true;
	}
	
	
	function submitForm3(){
		var questionone = $("#questionone").val().trim();
		var questiontwo = $("#questiontwo").val().trim();
		var answerone = $("#answerone").val().trim();
		var answertwo = $("#answertwo").val().trim();
		var pars ={questionone:questionone,questiontwo:questiontwo,answerone:answerone,answertwo:answertwo}
		var url =UrlConfig.updateQuestion;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == 1) {
	            	mui.alert("修改成功");                    
	                mui.back();     
	            } else {
	            	mui.alert("修改失败");               	
	            }
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败");
		  	}
		});
		
	}
	
	 //手机验证+身份证
    function submitForm2() {
        var card = $("#card2").val().trim();
		var code =$("#phonecode").val().trim();
		var smstype ="validPassword";
		var url =UrlConfig.checkPhoneCard;
		var pars={card:card,phone:code,smstype:smstype};
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
	            	mui.alert("验证成功"); 
	            	loadQuestion();
	            	$(".qbox").hide();
	                $(".qcode").hide();
					$(".qypwd").hide();
					$(".emailpwd").hide();
					$(".resetpwd").show();
	            } else if (status == "errorCard") {
	            	mui.alert("身份证号码错误");                	
	            } else if (status == "error") {
	            	mui.alert("手机验证错误"); 
	            } else if (status == "overTime") {
	            	mui.alert("手机验证码失效");        
	            } else {
	            	mui.alert("验证失败");               	
	            }
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败");
		  	}
		});
    }
    
    function loaddataSerucrity(){
	var url = UrlConfig.getSerucrity
	var guid = plus.storage.getItem(KeyCfg.userId);
	var pars={guid:guid};
	// 弹出系统等待对话框   
	//var w = mui.plus.nativeUI.showWaiting( "正在加载..." );
	MuiEventMgr.ajax(url,{pars:pars,
	  	sfn : function(data){
	  		var list = data.list;
	  		var answer1 = list[0];
	  		var answer2 = list[1];
	  		var question1 =answer1.problem;
	  		var question2 =answer2.problem;
	  		$("#questionone1").val(question1);
	  		$("#questiontwo2").val(question2);  		
	  	},ffn : function(err){
	  		mui.alert("获取数据失败");
	  		//mui.plus.nativeUI.closeWaiting();
	  	}
	 });
	
}
	
function loadQuestion(){
	var url=UrlConfig.getQuestion;
	MuiEventMgr.ajax(url,{pars:{},
	  	sfn : function(data){
	  		
			appendOp(data,"questionone");
			appendOp(data,"questiontwo");		
	  	},ffn : function(err){
	  		mui.alert("获取数据失败");
	  	}
	 });
}

	/**
	 *  为问题一和二添加答案 
	 * @param {Object} eleId
	 */
	function appendOp(data,eleId){
		var questions = data.questions;
	  	if(!questions) return;
		var arr = questions.split(";");
		var htmlArr = [];
		for(var i=0,count=arr.length; i<count; i++){
			var val = arr[i];
			htmlArr[i] = "<option value='"+val+"'>"+val+"</option>";
		}
		var op_htmls = htmlArr.join(" ");
		$("#"+eleId).append(op_htmls);
	}
	
	/*---------- 为问题一和二绑定检查事件 CODE START -----------*/
	var ele_questionone = CmwMcore.$('questionone');
	MeventMgr.onChange(ele_questionone,function(event,self){
		checkQuestion();
	});
	var ele_questiontwo = CmwMcore.$('questiontwo');
	MeventMgr.onChange(ele_questiontwo,function(event,self){
		checkQuestion();
	});
	
	var errMsg = null;
	function checkQuestion(){
		var q1_val = $("#questionone").val().trim();
		var q2_val = $("#questiontwo").val().trim();
		if(q1_val == q2_val){
			errMsg = "问题一和问题二不能相同！";
			mui.alert(errMsg);
			$("#questiontwo").val("--请选择--");
			return;
			q2_val.val("0");
		}else{
			errMsg = null;
		}
	}
});



